package net.zjitc.controller.client;

import com.sun.org.apache.xpath.internal.operations.Mod;
import net.zjitc.pojo.*;
import net.zjitc.service.CategoryService;
import net.zjitc.service.OrderItemService;
import net.zjitc.service.OrderService;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/5/13 8:52
 */
@Controller
public class MyController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    ProductService productService;
    public void top(Model model){
        List<Category> list = categoryService.findAll();
        model.addAttribute("list",list);

    }

    @RequestMapping(value = "/myAccount")
    public String myAccount(Model model){
        top(model);
        return "/client/myAccount.jsp";
    }

    @RequestMapping(value = "/modifyuserinfo")
    public String modifyuserinfo(Model model){
        top(model);
        return "/client/modifyuserinfo.jsp";

    }

    @RequestMapping(value = "/findOrderByUser")
    public String findByUser(Model model, HttpSession session){
        top(model);
        User user = (User) session.getAttribute("user");
        List<Orders> ordersList = orderService.findOrderByUser(user.getId());
        model.addAttribute("orders",ordersList);
        return "/client/orderlist.jsp";

    }

    @RequestMapping(value = "/findOrderById")
    public String findOrderById(Model model,String id){
        top(model);
        Orders orders= (Orders) orderService.findById(id);
        List<Orderitem> orderitemList = orderItemService.findByOId(id);
        for (Orderitem o:orderitemList){
            Product product = productService.findById(o.getPid());
            o.setProduct(product);

        }
        orders.setOrderitemList(orderitemList);
        model.addAttribute("order",orders);
        return "/client/orderInfo.jsp";


    }
    @RequestMapping(value = "/payui")
    public String payui(){
        return "/client/pay.jsp";
    }

    @RequestMapping(value = "/pay")
    public String pay(String id,Model model){
        top(model);
        orderService.update_state(id);
        return "/client/paysuccess.jsp";
    }

    @RequestMapping(value = "/delOrderById")
    public String delOrderById(String id,String type) {
        orderService.delete(id);
        return "redirect:/findOrderByUser";
    }


}
